package com.huazi.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huazi.commonutils.CommentResult;
import com.huazi.commonutils.JwtUtils;
import com.huazi.commonutils.MD5;
import com.huazi.commonutils.PhoneUtils;
import com.huazi.servicebase.exceptionhandler.MyException;
import com.huazi.ucenter.entity.Member;
import com.huazi.ucenter.entity.Register;
import com.huazi.ucenter.mapper.MemberMapper;
import com.huazi.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author huazi
 * @since 2021-03-27
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Override
    public String login(Member member) {

        //获取手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        //手机号和密码的非空校验
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password))
            throw new MyException("信息填写不完整！");
        if(!PhoneUtils.getFlag(mobile))
            throw new MyException("手机号格式不正确！");
        //根据手机号查询对象是否为空
        Member mem = baseMapper.selectOne(new QueryWrapper<Member>().eq("mobile", mobile));
        if(mem==null) throw new MyException("该手机号未注册！");
        //判断密码是否正确
        //因为存储到数据库的密码肯定是加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(mem.getPassword())) throw new MyException("密码不正确！");
        //判断用户是否可用
        if(mem.getIsDisabled()) throw new MyException("该用户不可用！");
        //到了这一步证明已经登录成功！
        //使用jwt工具类生成token
        String jwtToken = JwtUtils.getJwtToken(mem.getId(), mem.getNickname());//传入查询出来的member
        return jwtToken;
    }
    //会员注册
    @Override
    public CommentResult register(Register register) {
        //判断验证码是否正确
        //之前通过手机号存key
//        String code = redisTemplate.opsForValue().get(register.getMobile());
//        if(code==null) return CommentResult.getError().data("msg","验证码已过期！");
//        if(!code.equals(register.getCheckCode())) return CommentResult.getError().data("msg","验证码错误！");
        //非空校验
        if(StringUtils.isEmpty(register.getMobile())||StringUtils.isEmpty(register.getNickname())||StringUtils.isEmpty(register.getPassword())){
            System.out.println("not null");
            return  CommentResult.getError().data("msg","注册失败！");
        }
        //手机号校验
        if(!PhoneUtils.getFlag(register.getMobile())) {
            System.out.println("手机号码格式不正确");
            return  CommentResult.getError().data("msg","注册失败！手机号码格式不正确");
        }
        //密码校验
        if(register.getPassword().length()<6) return  CommentResult.getError().data("msg","注册失败！密码长度低于6位");
        //判断手机号是否已经注册
        Integer integer = baseMapper.selectCount(new QueryWrapper<Member>().eq("mobile", register.getMobile()));
        if(integer!=0) {
            return CommentResult.getError().data("msg","该手机号已注册！");
        }
        Member member=new Member();
        //密码加密
        String encrypt = MD5.encrypt(register.getPassword());
        member.setMobile(register.getMobile())
                .setPassword(encrypt)
                .setNickname(register.getNickname())
                .setIsDisabled(false)
                ;
        int insert = baseMapper.insert(member);
//        System.out.println("~~"+insert);
        if(insert <=0) new MyException("服务出错");
        return CommentResult.getOK();
    }
    //
    @Override
    public Member getOpenIdMember(String openid) {
        Member member = baseMapper.selectOne(new QueryWrapper<Member>().eq("openid", openid));
        return member;
    }
    //获取某天的注册人数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }

}
